<template>
    <div>
<!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
<!-- 卡片视图区域 -->
    <el-card>
         <el-row :gutter="20">
        <el-col :span="8">
          <!-- 搜索与添加区域 -->
          <el-input placeholder="请输入内容"  clearable v-model="queryInfo.query" clear @clear="getGoodsList()">
            <el-button slot="append" icon="el-icon-search" @click="search" title="搜索"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodsList" border stripe>
          <el-table-column type="index"></el-table-column>
          <el-table-column label="商品名称" prop="goods_name"></el-table-column>
          <el-table-column label="价格(元)" prop="goods_price" width="90px"></el-table-column>
          <el-table-column label="商品重量" prop="goods_weight" width="90px"></el-table-column>
          <el-table-column label="创建时间" prop="add_time" width="200px">
               <template slot-scope="scope">
                  {{scope.row.add_time*1050 | dateFormat}}
              </template>
          </el-table-column>
          <el-table-column width="190px" label="操作">
                <template slot-scope="scope">
              <el-button icon="el-icon-edit" size="mini" type="primary" @click="showEditDialog">
                  编辑</el-button>
              <el-button icon="el-icon-delete" size="mini" type="danger" @click="removeGoods(scope.row)">
                  删除</el-button>
          </template>
          </el-table-column>
      </el-table>
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="this.total" background>
    </el-pagination>
    </el-card>
    <!-- 编辑商品的对话框 -->
     <el-dialog title="修改商品属性" :visible.sync="editGoodsdialogVisible"
  width="50%" @close="editGoodsClosed">
<el-form :model="editGoodsForm" :rules="editGoodsRules" ref="editGoodsRef" label-width="100px">
  <el-form-item label="商品名称" prop="goods_name">
    <el-input ></el-input>
  </el-form-item>
   <el-form-item label="商品价格" prop="goods_price">
    <el-input ></el-input>
  </el-form-item>
   <el-form-item label="商品重量" prop="goods_weight">
    <el-input v-model="editGoodsForm.goods_weight"></el-input>
  </el-form-item>
</el-form>
 <span slot="footer" class="dialog-footer">
    <el-button @click="editGoodsdialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editGoodsTrue">确 定</el-button>
  </span>
</el-dialog>

    </div>
</template>

<script>
export default {
  data () {
    return {
      goodsForm: {

      },
      goodsList: [], // 商品列表
      queryInfo: {
        // 查询参数对象
        query: '', // 搜索参数
        pagenum: 1,
        pagesize: 10
      },
      total: 0, // 总数据条数
      editGoodsdialogVisible: false,
      editGoodsForm: {
        goods_name: '',
        goods_price: '',
        goods_weight: '',
        start_time: ''

      },
      editGoodsRules: {}

    }
  },
  methods: {
    addGoods () {
      // 添加商品
      this.$router.push('/goods/add');
    },
    async getGoodsList () {
      // 请求商品数据
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('请求商品数据失败');
      }
      this.goodsList = res.data.goods;
      this.total = res.data.total;
      // console.log(res.data);
    },
    search () {
      this.getGoodsList();
    },
    handleSizeChange (newsize) {
      this.queryInfo.pagesize = newsize;
      this.getGoodsList()
    },
    handleCurrentChange (newpage) {
      this.queryInfo.pagenum = newpage;
      this.getGoodsList();
    },
    async removeGoods (data) {
    // 删除商品
      const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除!');
      }
      const { data: res } = await this.$http.delete(`goods/${data.goods_id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败');
      }
      this.$message.success('删除成功');
      this.getGoodsList();
    },
    showEditDialog () {
      // 控制编辑商品对话框
      this.editGoodsdialogVisible = true;
    },
    editGoodsTrue () {

    },
    addGoodsTrue () {

    }

  },
  created () {
    this.getGoodsList();
  },
  computed: {

  }
}
</script>
<style lang="less" scoped>

</style>
